package com.shen.JUC.volatileTest;

import java.util.concurrent.atomic.AtomicInteger;

public class volatileDemo2 {
    //不保证原子性
    private volatile static Long num1 = 0L;
    //原子类的Integer,可以保证原子性
//    private volatile static AtomicInteger num1=new AtomicInteger();
    public static void add() {
        num1++;
//      num1.getAndIncrement();//CAS操作
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {//main正常是2w
            new Thread(() -> {
                for (int i1 = 0; i1 < 1000; i1++) {
                    add();
                }
            }).start();
        }
        while (Thread.activeCount() > 2) { //main,gc
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName() + " " + num1);
    }
}
